home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / datacomm / xpr / xprkermit-1.111.lzh / timer.c < prev    next >
C/C++ Source or Header  |  1991-10-12  |  2KB  |  77 lines

  1. /*
  2.  * Timer.device routines from RKM, slightly modified.
  3.  *
  4.  * Version 1.88:  Renamed routine TimeDelay() to MyDelay(), since the former
  5.  * conflicts with a routine of the same name in the 2.0 amiga.lib.
  6.  */
  7.  
  8. #ifndef _lint
  9. static char rcsid[] = "$Header$";
  10. #endif
  11.  
  12. #include "timer.h"
  13. #include <functions.h>
  14.  
  15. void
  16. DeleteTimer(struct timerequest *tr) {
  17.    struct MsgPort *tp;
  18.  
  19.    if (tr != 0) {
  20.       tp = tr->tr_node.io_Message.mn_ReplyPort;
  21.       if (tp != 0)
  22.          DeletePort(tp);
  23.       CloseDevice((struct IORequest *) tr);
  24.       DeleteExtIO((struct IORequest *) tr);
  25.    }
  26. }
  27.  
  28. struct timerequest *
  29. CreateTimer(ULONG unit) {
  30.    /* return a pointer to a timer request.  If any problem, return NULL. */
  31.  
  32.    LONG error;
  33.    struct MsgPort *timerport;
  34.    struct timerequest *timermsg;
  35.    
  36.    timerport = CreatePort(0, 0);
  37.    if (timerport == NULL)
  38.       return NULL;
  39.    timermsg = (struct timerequest *)
  40.               CreateExtIO(timerport, sizeof(struct timerequest));
  41.    if (timermsg == NULL)
  42.       return NULL;
  43.    error = OpenDevice((UBYTE *) TIMERNAME, unit,
  44.                       (struct IORequest *) timermsg, 0L);
  45.    if (error != 0) {
  46.       DeleteTimer(timermsg);
  47.       return NULL;
  48.    }
  49.    return timermsg;
  50. }
  51.  
  52. void
  53. WaitForTimer(struct timerequest *tr, struct timeval *tv) {
  54.    /*----------------------------------------------*/
  55.    /* With the UNIT_MICROHZ timer, it is illegal   */
  56.    /* to wait for 0 or 1 microseconds!             */
  57.    /*----------------------------------------------*/
  58.    if (tv->tv_secs == 0L && tv->tv_micro < 2L) return;
  59.    
  60.    tr->tr_node.io_Command = TR_ADDREQUEST;    /* add a new timer request */
  61.  
  62.    /* Post request to the timer--will go to sleep till done. */
  63.    DoIO((struct IORequest *) tr);
  64. }
  65.  
  66. /* More precise timer than AmigaDOS Delay() */
  67.  
  68. LONG MyDelay(struct timeval *tv, LONG unit) {
  69.    struct timerequest *tr;
  70.    
  71.    tr = CreateTimer(unit);
  72.    if (tr == NULL) return -1L;
  73.    WaitForTimer(tr, tv);
  74.    DeleteTimer(tr);
  75.    return 0L;
  76. }
  77.